﻿using System;
using System.Collections.Generic;
using IInject.Internal;
using IInject.Resolution;

namespace IInject.Registration
{
	/// <summary>
	/// 
	/// </summary>
	public class RegistryContext : DisposableContainerMember
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="RegistryContext"/> class.
		/// </summary>
		public RegistryContext() : this(ContainerProvider.Current)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="RegistryContext"/> class.
		/// </summary>
		public RegistryContext(IContainer container) : base(container)
		{
			Properties = new List<InjectPropertyContext>();
		}

		/// <summary>
		/// Gets or sets the key.
		/// </summary>
		/// <value>
		/// The key.
		/// </value>
		public string Key { get; set; }

		/// <summary>
		/// Gets or sets from type.
		/// </summary>
		/// <value>
		/// From type.
		/// </value>
		public Type FromType { get; set; }

		/// <summary>
		/// Gets or sets to type.
		/// </summary>
		/// <value>To type.</value>
		public Type ToType { get; set; }

		/// <summary>
		/// Gets or sets the scope.
		/// </summary>
		/// <value>The scope.</value>
		public Scope Scope { get; set; }

		/// <summary>
		/// Gets or sets the instance.
		/// </summary>
		/// <value>
		/// The instance.
		/// </value>
		public virtual object Instance { get; set; }

		/// <summary>
		/// Gets or sets the dependency.
		/// </summary>
		/// <value>
		/// The dependency.
		/// </value>
		public TypeDependency Dependency { get; set; }

		/// <summary>
		/// Gets the properties.
		/// </summary>
		/// <value>
		/// The properties.
		/// </value>
		public List<InjectPropertyContext> Properties { get; private set; }

		/// <summary>
		/// Gets a value indicating whether this instance is generic type.
		/// </summary>
		/// <value>
		/// <c>true</c> if this instance is generic type; otherwise, <c>false</c>.
		/// </value>
		public bool IsGenericType
		{
			get
			{
				return ToType.IsGenericTypeDefinition;
			}
		}

		/// <summary>
		/// When overridden in a derived class, disposes of this instances' allocated resources.
		/// </summary>
		/// <param name="disposing"></param>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				object obj = Instance;

				if (obj != null && obj is IDisposable)
				{
					((IDisposable)Instance).Dispose();
				}
			}
		}
	}
}
